import axios, { Method } from 'axios'
import QS from 'qs'
import store from '../store'
import { message as Message } from 'ant-design-vue'
import { getToken } from '@/utils/cookie'

const baseUrl = process.env.VUE_APP_BASE_API

const service = axios.create({
  baseURL: baseUrl, // api 的 base_url
  withCredentials: false
})

// 请求拦截器
service.interceptors.request.use((config) => {
  const token = getToken()
  // token携带
  if (token) {
    config.headers.Authorization = token
  }
  if (config.headers['Content-Type'] !== 'application/json;charset=UTF-8') {
    // 数据序列化
    config.data = QS.stringify(config.data)
  }
  return config
}, (error) => {
  Message.error(`请求拦截器${error}`)
  return Promise.reject(error)
})

// 响应拦截器
service.interceptors.response.use((res) => {
  if (res && res.data) {
    return Promise.resolve(res.data)
  } else {
    // Message.error(res.data)
  }
  return res
}, (err) => {
  // 响应结束
  // loadingInstance.close()
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        err.message = '错误请求'
        break
      case 401:
        err.message = 'session过期，请从新登陆'
        Message.error(err.message)
        store.dispatch('user/logout')
        break
      case 403:
        err.message = '拒绝访问'
        break
      case 404:
        err.message = '请求错误,未找到该资源'
        break
      case 405:
        err.message = '请求方法未允许'
        break
      case 408:
        err.message = '请求超时'
        break
      case 500:
        err.message = '服务器端出错'
        break
      case 501:
        err.message = '网络未实现'
        break
      case 502:
        err.message = '网络错误'
        break
      case 503:
        err.message = '服务不可用'
        break
      case 504:
        err.message = '网络超时'
        break
      case 505:
        err.message = 'http版本不支持该请求'
        break
      default:
        err.message = `连接错误${err.response.status}`
    }
  } else {
    err.message = '连接到服务器失败'
  }
  // Message.closeAll()
  Message.error(err.message)
})

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const baseMethod = (method: Method, url: string, params?: object): Promise<any> => {
  return new Promise((resolve, reject) => {
    service({
      method,
      url,
      params
    }).then((res) => {
      resolve(res)
    }).catch((error) => {
      reject(error)
    })
  })
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const post = (url: string, data?: object, headers = false): Promise<any> => {
  return new Promise((resolve, reject) => {
    service({
      method: 'POST',
      url,
      data,
      headers: {
        'Content-Type': headers ? 'application/x-www-form-urlencoded;charset=UTF-8' : 'application/json;charset=UTF-8'
      }
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

export const get = (url: string, params?: object) => baseMethod('GET', url, params)

export const deletes = (url: string, params: object) => baseMethod('DELETE', url, params)
